<?php
// +----------------------------------------------------------------------
// | PHP [ JUST YOU ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2017 http://www.jyphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: FlyingPig <flyingpigfp@163.com>
// +----------------------------------------------------------------------

namespace App\Logic;


use App\Common\Logger;
use App\Common\Sql;
use App\Common\Tool;
use Illuminate\Support\Facades\DB;

class LoginLogic extends Logic
{

    /**
     * 登录后的后置操作
     * @param $param
     * @return array
     */
    public function postPosition($param)
    {
        $skey = Tool::skey();

        DB::table('t_player_info')->where('id', $param['id'])->update([
            'login_ip' => $_SERVER["REMOTE_ADDR"],
            'skey' => $skey,
            'update_time' => date('Y-m-d H:i:s'),
            'avater_url' => $param['avater_url']
        ]);
        DB::table('t_login_rec')->insert([
            'login_uid' => $param['id'],
            'login_type' => $param['type'],
            'login_ip' => $_SERVER["REMOTE_ADDR"],
        ]);

        //如果是VIP会员,每日首次登陆奖励相应的奖品
        if (
            $param['update_time'] < date('Y-m-d')
            &&
            $goods_id = DB::table('t_vip_info')->where('uid', $param['id'])->where('expire_time', '>', date('Y-m-d H:i:s'))->value('vip_conf_id')
        ) {
            VipLogic::instance()->vipConfPostPosition($goods_id,$param['id'],'activity_conf_type_login');
            $user_object          = DB::table('t_player_info')->where('id', $param['id'])->first(['gold', 'diamonds', 'room_ticket']);
            $param['gold']        = $user_object->gold;
            $param['diamonds']    = $user_object->diamonds;
            $param['room_ticket'] = $user_object->room_ticket;
        }
        //VIP信息
        $vip_info = VipLogic::instance()->vipInfo($param['id']);
        //组合要返回的内容
        $param['skey']          = $skey;
        $param['is_first_time'] = $param['update_time'] < date('Y-m-d') ? 1 : -1;
        $param['vip_level']     = $vip_info['level'];
        $param['vip_name']      = $vip_info['name'];
        $param['vip_day'] = VipLogic::instance()->conversionDay($vip_info['expire_time']);
        $param['is_binding'] = DB::table('t_users_binding')->where('uid',$param['id'])->value('uid') ? 1 : 0;
        return $this->getReturnData($param);
    }



    //记录登录日志
    public function insertLoginRec($uid, $type)
    {
        Sql::insert('t_login_rec', [
            "login_uid" => $uid,
            'login_type' => $type,
            'login_ip' => $_SERVER["REMOTE_ADDR"],
        ]);
    }


    //获取登录后统一返回的参数
    public function getReturnData($param)
    {
        //规定要返回的字段
        $response = [
            'uid', 'nick_name', 'sex', 'avater_url', 'gold', 'diamonds', 'room_ticket', 'login_ip', 'skey', 'login_type', 'paiwei_score', 'paiwei_rank',
            'paiwei_rank_name', 'is_first_time', 'vip_level', 'vip_name', 'win_board', 'fail_board', 'peace_board','is_binding','vip_day'
        ];

        $ret_arr = $this->cal_paiwei_rank($param['paiwei_score']);
        //组合要返回的内容
        $param['paiwei_rank']      = $ret_arr["paiwei_rank"];
        $param['paiwei_rank_name'] = '敬请期待';
        $param['uid']              = $param['id'];
        $param['login_type']       = $param['type'];
        $param['login_ip']         = $_SERVER["REMOTE_ADDR"];
        $param['paiwei_score']     = 0;
        foreach ($param as $key => $value) {
            if (!in_array($key, $response)) {
                unset($param[$key]);
            }
        }
        return $param;
    }


    private function cal_paiwei_rank($paiwei_score)
    {
        $row = Sql::select('t_rank_conf', [['min_score', '<=', $paiwei_score], ['max_score', '<', $paiwei_score]]);
        if (count($row) <= 0) {
            Logger::instance()->error(Tool::getUri(), "get paiwei error.\n");
            $rank                     = array();
            $rank['paiwei_rank']      = 0;
            $rank['paiwei_rank_name'] = "无";
            return $rank;
        }
        $rank                     = array();
        $rank['paiwei_rank']      = (int)$row[0]["id"];
        $rank['paiwei_rank_name'] = $row[0]["name"];
        return $rank;
    }





}